package com.gitee.somenvm.ninerpgitem.compat.attribute

import com.gitee.somenvm.ninerpgitem.util.info
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.entity.LivingEntity
import org.bukkit.inventory.ItemStack
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.function.console
import java.util.*

/**
 *
 * @author  SomeNvM
 * @date  2022/12/12 17:54
 */
interface AttributeBridge {

    companion object {
        private val inspects = mutableListOf(
            Inspect(arrayOf("OriginAttribute"), OriginAttributeBridge) { isEnable },
            Inspect(arrayOf("AttributePlus3"), AttributePlus3Bridge) {
                (Bukkit.getPluginManager().getPlugin("AttributePlus")?.description?.version?.split(".")?.get(0)
                    ?: "-1") == "3"
            },
            Inspect(arrayOf("NULL"), AttributeNullBridge) { true }
        )

        val INSTANCE: AttributeBridge? by lazy { createBridge() }

        @Awake(LifeCycle.ENABLE)
        fun createBridge(): AttributeBridge? {
            val inspect = inspects.firstOrNull { it.check(it) } ?: return null
            console().cast<CommandSender>().info("|- Attribute drive lock to [${inspect.names.joinToString(",")}]")
            return inspect.bridge
        }
    }

    class Inspect(val names: Array<String>, val bridge: AttributeBridge, val check: Inspect.() -> Boolean) {
        val isEnable: Boolean
            get() = names.any { Bukkit.getPluginManager().isPluginEnabled(it) }
    }

    fun addAttributes(uuid: UUID, reads: List<String>)

    fun addAttributes(source: String, uuid: UUID, reads: List<String>)

    fun removeAttributes(uuid: UUID, source: String)

    fun getAttributeValue(uuid: UUID, keyword: String): Number

    fun getAttributeValue(itemStack: ItemStack, keyword: String): Number

    fun getAttributeValue(list: MutableList<String>, keyword: String): Number

    fun update(entity: LivingEntity)

    fun update(uuid: UUID)
}